package com.example.administrator.myproject.Utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 类描述:
 * 创建人:一一哥
 * 创建时间:16/11/17 14:17
 * 备注:
 */

public class HttpUtil {
    //下载数据
    public static byte[] loadData(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream is = conn.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int len = 0;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                    baos.flush();
                }
                byte[] data = baos.toByteArray();

                baos.close();
                is.close();
                return data;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
